package com.ptrsystem.siatd.controllers;

import com.ptrsystem.siatd.beans.DocumentoIngresoExt;
import com.ptrsystem.siatd.dao.logica.MantenimientoAnnioLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoDocumentoIngresoExtLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoDocumentoLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoEstadoDocIngExtLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoTipUsuarioExtLogica;
import java.util.Formatter;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class DocumentoIngresoExtController {

    @Autowired
    private MantenimientoDocumentoIngresoExtLogica logica;
    @Autowired
    private MantenimientoEstadoDocIngExtLogica estadoDocIngExt;
    @Autowired
    private MantenimientoAnnioLogica anio;
    @Autowired
    private MantenimientoTipUsuarioExtLogica usuExt;
    @Autowired
    private MantenimientoDocumentoLogica documento;

    @RequestMapping(value = "registrarDocumentoIngExt.htm", method = RequestMethod.GET)
    private String registrarDocIngExt(HttpServletRequest req) {
        int nroDoc = logica.generaNroDocIngExt(String.valueOf(req.getSession(true).getAttribute("dni")));
        Formatter nrodocF = new Formatter();
        nrodocF.format("%06d", nroDoc);
        req.getSession(true).setAttribute("doc", documento.listaDocumento());
        req.getSession(false).setAttribute("anio", anio.listaAnio());
        req.getSession(false).setAttribute("tipUsuExt", usuExt.getListaTipUsuarioExt());
        req.getSession(false).setAttribute("estDocIngExt", estadoDocIngExt.listaEstadoDocIngExt());
        req.getSession(false).setAttribute("codTipDocIngExt", logica.generaCodDocIngExt());
        req.getSession(false).setAttribute("nroTipDocIngExt", nrodocF);
        req.getSession(false).setAttribute("result", "Estado De Mantenimiento");
        return "Mantenimiento/NRegDocumentoIngExt";
    }

    @RequestMapping(value = "registraDocumentoIngExt.htm", method = RequestMethod.POST)
    private String registraDocIngExt(DocumentoIngresoExt docIngExt, HttpServletRequest req) {
        docIngExt.setCodTipoDocIngExt(String.valueOf(logica.generaCodDocIngExt()));
        int nroDoc = logica.generaNroDocIngExt(String.valueOf(req.getSession(true).getAttribute("dni")));
        Formatter nrodocF = new Formatter();
        nrodocF.format("%06d", nroDoc);
        docIngExt.setNroTipDocIngExt(String.valueOf(nrodocF));
        boolean rs = logica.registraDocIngExt(docIngExt);
        if (rs) {
            req.getSession(false).setAttribute("result", "Registro Efectuado Correctamente");
            req.getSession(false).setAttribute("codTipDocIngExt", logica.generaCodDocIngExt());
            req.getSession(false).setAttribute("nroTipDocIngExt", logica.generaNroDocIngExt(String.valueOf(req.getSession(true).getAttribute("dni"))));
        } else {
            req.getSession(false).setAttribute("result", "Registro No Efectuado");
        }
        return "Mantenimiento/NRegDocumentoIngExt";
    }

}
